import 'package:flutter/material.dart';

class InputView extends StatefulWidget {
  double font;
  Color textColor;
  Color cursorColor;
  double contentPadding;
  TextEditingController? controller;
  TextAlign textAlign;
  void Function(String)? onChange;
  void Function(String)? onCompletion;
  String? Function(String?)? validator;
  InputView(
      {super.key,
      this.font = 13,
      this.textColor = Colors.black,
      this.cursorColor = Colors.black,
      this.contentPadding = 13,
      this.textAlign = TextAlign.left,
      this.controller,
      this.onChange,
      this.onCompletion,
      this.validator});

  @override
  State<InputView> createState() => InputViewState();
}

class InputViewState extends State<InputView> {
  @override
  Widget build(BuildContext context) {
    return TextFormField(
      style: TextStyle(fontSize: widget.font, color: widget.textColor),

      controller: widget.controller,
      onChanged: widget.onChange,
      // onSubmitted: widget.onCompletion,
      validator: widget.validator,
      autovalidateMode: AutovalidateMode.always,

      cursorColor: widget.cursorColor,
      textAlign: widget.textAlign,
      onEditingComplete: () {
        if (widget.onCompletion != null && widget.controller != null) {
          widget.onCompletion!(widget.controller!.text);
        }
      },

      ///边框设置
      decoration: InputDecoration(
        // isDense: true, // 密集模式，减少内部空间
        helperText: widget.validator != null ? '' : null,

        ///这里添加空字符串占位，避免验证失败下划线和无输入状态时下划线，高低位置会变化
        contentPadding: EdgeInsets.symmetric(
            vertical: widget.contentPadding), // 调整垂直内边距以使文本居中
        border: UnderlineInputBorder(
            borderSide:
                BorderSide(color: widget.textColor, width: 0.5)), // 设置无边框
        enabledBorder: UnderlineInputBorder(
            borderSide:
                BorderSide(color: widget.textColor, width: 0.5)), // 输入框启用时无边框
        focusedBorder: UnderlineInputBorder(
            borderSide:
                BorderSide(color: widget.textColor, width: 0.5)), // 输入框获得焦点时无边框
        focusedErrorBorder: const UnderlineInputBorder(
            borderSide:
                BorderSide(color: Colors.red, width: 1)), // 输入框获得焦点，且验证失败时无边框
        errorStyle: const TextStyle(
          color: Colors.red, // 错误提示文字颜色
          fontSize: 12, // 错误提示文字大小
        ),
      ),
    );
  }
}
